package com.syh.jn.scm.infrastructure.repository.impl.supplier.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AggregateRepoHibernateImplTmpl;
import com.syh.jn.scm.domain.supplier.entity.finance.StatementAccountBillCart;
import com.syh.jn.scm.domain.supplier.repository.finance.StatementAccountBillCartRepository;
import com.syh.jn.scm.infrastructure.repository.model.finance.StatementAccountBillCartModel;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToHQL;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 对账单临时表仓储
 */
@Repository
public class StatementAccountBillCartRepositoryImpl implements StatementAccountBillCartRepository {
  private HibernateTemplate hibernateTemplate;
  private AggregateRepoHibernateImplTmpl<StatementAccountBillCart, StatementAccountBillCartModel,Long> statementAccountBillCartHibernateImpl;

  @Autowired
  public StatementAccountBillCartRepositoryImpl(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
    this.statementAccountBillCartHibernateImpl =
            new AggregateRepoHibernateImplTmpl.Builder().hibernateTemplate(this.hibernateTemplate).entityClass(StatementAccountBillCart.class).modelClass(StatementAccountBillCartModel.class).build();
  }


  /**
   *  批量添加 添加前删除旧数据
   * @param statementAccountBillCarts
   * @param wrapper
   */
  @Override
  public void add(List<StatementAccountBillCart> statementAccountBillCarts,Wrapper wrapper) {
    this.remove(wrapper);
    statementAccountBillCarts.forEach(statementAccountBillCart-> {
      this.statementAccountBillCartHibernateImpl.add(statementAccountBillCart);
    });
  }

  /**
   * 按条件删除
   * @param wrapper
   */
  @Override
  public void remove(Wrapper wrapper){
    ResolveToHQL parse = new ResolveToHQL(wrapper);
    StringBuffer hql = new StringBuffer();
    hql.append("delete StatementAccountBillCartModel ");
    hql.append(" WHERE 1=1 ");
    hql.append(parse.resolve());
    Query query = this.statementAccountBillCartHibernateImpl.buildQuery(hql);
    query.executeUpdate();
  }

  @Override
  public  List<StatementAccountBillCart> getList(Wrapper wrapper) {
    return this.statementAccountBillCartHibernateImpl.getList(wrapper);
  }
}
